<?php

namespace App\Http\Controllers\Api;

use App\Comment;
use App\Order;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DashboardController extends Controller
{
    public function index()
    {
        $date = [
            0 => Carbon::now()->subDays(6)->format('Y-m-d'),
            1 => Carbon::now()->subDays(5)->format('Y-m-d'),
            2 => Carbon::now()->subDays(4)->format('Y-m-d'),
            3 => Carbon::now()->subDays(3)->format('Y-m-d'),
            4 => Carbon::now()->subDays(2)->format('Y-m-d'),
            5 => Carbon::now()->subDays(1)->format('Y-m-d'),
            6 => Carbon::now()->format('Y-m-d'),
        ];
        $date2 = [
            0 => Carbon::now()->subDays(6)->format('d日'),
            1 => Carbon::now()->subDays(5)->format('d日'),
            2 => Carbon::now()->subDays(4)->format('d日'),
            3 => Carbon::now()->subDays(3)->format('d日'),
            4 => Carbon::now()->subDays(2)->format('d日'),
            5 => Carbon::now()->subDays(1)->format('d日'),
            6 => Carbon::now()->format('d日'),
        ];

        // 最近7天统计
        $orders = [];
        $users = [];
        $comments = [];
        foreach ($date as $key => $day) {
            $orders[$key] = Order::where('pay_status', '<>', 0)->whereDate('created_at', $day)->count();
            $users[$key] = User::whereDate('created_at', $day)->count();
            $comments[$key] = Comment::whereDate('created_at', $day)->count();
        }

        return response([
            'date' => $date2,
            'orders' => $orders,
            'users' => $users,
            'comments' => $comments,
        ]);
    }
}
